package com.sohh.demo;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: ruoan
 * @date: 2021/5/16 15:57
 */
public class Test11 {

    public static void main(String[] args) {

        boolean flag = true;

        List<Student> studentList = new ArrayList<>();
        List<Classroom> classroomList = new ArrayList<>();

        Classroom classroom = new Classroom();
        classroom.setRoomId(111);
        classroom.setRoomName("name+" + 111);
        Classroom classroom2 = new Classroom();
        classroom2.setRoomId(222);
        classroom2.setRoomName("name+" + 222);

        classroomList.add(classroom);
        classroomList.add(classroom2);

        Student student;
        for (int i = 0; i < 10; i++) {
            student = new Student();
            student.setRoomId(i);
            student.setName("name+" + i);

            if (flag) {
                student.setRoomId(classroom.getRoomId());
            } else {
                student.setRoomId(classroom2.getRoomId());
            }
            flag = !flag;

            studentList.add(student);
        }

        //为每个学生赋值他所在的教室
        MergeUtil.merge(studentList,classroomList,(stu,room) -> stu.getRoomId() == room.getRoomId(),(stu,room) -> stu.setRoomName(room.getRoomName()));

        studentList.forEach((data) -> {
            System.out.println(data.toString());
        });

        //为每个教室，赋值属于他的学生
        classroomList.stream().forEach(room -> {
            List dataList = studentList.stream().filter(student1 -> student1.getRoomId() == room.getRoomId() ).collect(Collectors.toList());
            room.setStudentList(dataList);
        });
//
        System.out.println(classroomList);

        // 使用工具类
        //为每个教室，赋值属于他的学生
        MergeUtil.mergeList(classroomList,studentList,
                (classroom1, student1) -> classroom1.getRoomId() == student1.getRoomId(),
                (classroom1, students) -> classroom1.setStudentList(students));

        System.out.println(classroomList);

    }



}
